//
//  BuySucceedController.swift
//  SCA
//
//  Created by tjun on 2025/9/19.
//

import UIKit
import SnapKit

class BuySucceedController: UIViewController {
    
    private var maskView: UIButton!
    private var contentView: UIView!
    
    let messsage: String
    
    init(messsage: String) {
        self.messsage = messsage
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .custom
    }
    
    var onDismiss: (() -> Void)?
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showIn(_ controller: UIViewController) {
        controller.present(self, animated: false, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.backgroundColor = .clear
        
        maskView = UIButton(frame: view.bounds).addTo(view)
        maskView.backgroundColor = .black.withAlphaComponent(0)
        
        let contentW = view.width - 80
        var contentH: CGFloat = 350
        
        contentView = UIView().addTo(view)
        contentView.backgroundColor = UIColorMake(0x212529)
        contentView.frame = CGRectMake((view.width - contentW) * 0.5, -contentH , contentW, contentH)
        contentView.addCorner(radius: 15)
        
        let iconImageView = UIImageView(image: UIImage(named: "buySucceed"))
        iconImageView.addTo(contentView)
        iconImageView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(34)
            make.width.height.equalTo(84.scaled)
        }

        //        let text = """
        //        恭喜您已解锁盈利通道，全速驶向财富高峰！\n专属您的收益已就位，直通3.2倍收益通道！\n投入 $600 轻松收获 $1,920.00，总收益轻松到手！\n您的创富之旅，从智云时代开始！
        //        """
        //
        
        let textLabel = UILabel().addTo(contentView)
        textLabel.numberOfLines = 0
        
        if let data = messsage.data(using: .utf8) {
            
            let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
                .documentType: NSAttributedString.DocumentType.html,
                .characterEncoding: String.Encoding.utf8.rawValue
            ]
            
            if let attributedText = try? NSMutableAttributedString(data: data, options: options, documentAttributes: nil) {
                let fullRange = NSRange(location: 0, length: attributedText.length)
                attributedText.addAttributes([
                    .font: UIFont.systemFont(ofSize: 16, weight: .medium),
                    .foregroundColor: UIColor.white,
                ], range: fullRange)
                
                textLabel.attributedText = attributedText
            }
        }
        
        textLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(iconImageView.snp.bottom).offset(25)
        }
        
        let confirmButton = MainButton(type: .system, title: "OK", font: UIFontMake(16), textColor: .white)
        confirmButton.addTo(contentView)
        confirmButton.addCorner(radius: 10)
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        confirmButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(textLabel)
            make.height.equalTo(AppStyle.size.smallButtonHeight.scaled)
            make.top.equalTo(textLabel.snp.bottom).offset(10)
        }
        
        view.setNeedsLayout()
        view.layoutIfNeeded()

        contentH = CGRectGetMaxY(confirmButton.frame) + 30
        contentView.frame = CGRectMake((view.width - contentW) * 0.5, -contentH , contentW, contentH)
    }
     
    @objc func confirmButtonTapped() {
        dismiss(completion: onDismiss)
    }
 
    private func dismiss(completion: (() -> Void)? = nil) {
        UIView.animate(withDuration: 0.25) {
            self.contentView.alpha = 0
            self.maskView.backgroundColor = .black.withAlphaComponent(0)
            self.contentView.y = self.contentView.y + 100
        } completion: { finishi in
            self.dismiss(animated: false, completion: completion)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
 
 
        UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: .curveEaseIn) {
            self.maskView.backgroundColor = .black.withAlphaComponent(0.5)
            self.contentView.y = (self.view.height - self.contentView.height) * 0.5 - 35
        }
    }
    
    
}

